﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:src="clr-namespace:DataGridExtensions"
                    xmlns:behaviors="clr-namespace:DataGridExtensions.Behaviors"
                    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">

  <!-- Style for a filter check box. The check box is only visible when it's hovered, focused or has a value.  -->
  <Style x:Key="{ComponentResourceKey {x:Type src:DataGridFilter}, ColumnHeaderSearchCheckBoxStyle}" TargetType="CheckBox">
    <Setter Property="IsThreeState" Value="True" />
    <Setter Property="Margin" Value="4,0,2,0" />
    <Style.Triggers>
      <Trigger Property="CheckBox.IsChecked" Value="{x:Null}">
        <Setter Property="Opacity" Value="0" />
      </Trigger>
      <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Opacity" Value="1" />
      </Trigger>
      <Trigger Property="IsFocused" Value="True">
        <Setter Property="Opacity" Value="1" />
      </Trigger>
    </Style.Triggers>
  </Style>

  <!-- Style for the clear button in the filter text box -->
  <Style x:Key="{ComponentResourceKey {x:Type src:DataGridFilter}, ColumnHeaderSearchTextBoxClearButtonStyle}" TargetType="Button">
    <Setter Property="Foreground" Value="LightGray" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ButtonBase">
          <Border Background="Transparent">
            <Path Data="M0,0 L7,7 M0,7 L7,0" Stroke="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center" StrokeThickness="2" Margin="3" />
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Foreground" Value="Gray" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!-- Style for a filter text box. The text box is only visible when it's hovered, focused or has a value.  -->
  <Style x:Key="{ComponentResourceKey {x:Type src:DataGridFilter}, ColumnHeaderSearchTextBoxStyle}" TargetType="TextBox">
    <Setter Property="MinWidth" Value="20" />
    <Setter Property="Margin" Value="4,0,2,0" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type TextBox}">
          <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
            <DockPanel>
              <Button x:Name="clear" DockPanel.Dock="Right" Focusable="False" Style="{DynamicResource {x:Static src:DataGridFilter.ColumnHeaderSearchTextBoxClearButtonStyleKey}}">
                <i:Interaction.Behaviors>
                  <behaviors:ClearTextBoxOnButtonClickBehavior TextBox="{Binding RelativeSource={RelativeSource TemplatedParent}}" />
                </i:Interaction.Behaviors>
              </Button>
              <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" />
            </DockPanel>
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="False">
              <Setter Property="Opacity" TargetName="border" Value="0.56" />
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA" />
            </Trigger>
            <Trigger Property="IsKeyboardFocused" Value="True">
              <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5" />
            </Trigger>
            <Trigger Property="Text" Value="">
              <Setter Property="Visibility" TargetName="clear" Value="Hidden" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
    <Style.Triggers>
      <Trigger Property="Text" Value="">
        <Setter Property="Opacity" Value="0" />
      </Trigger>
      <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Opacity" Value="1" />
      </Trigger>
      <Trigger Property="IsFocused" Value="True">
        <Setter Property="Opacity" Value="1" />
      </Trigger>
    </Style.Triggers>
  </Style>

  <!-- Reusable filter symbol -->
  <ControlTemplate x:Key="{ComponentResourceKey {x:Type src:DataGridFilter}, IconTemplate}">
    <Path Data="M0,0 L10,0 6,4 6,9 4,9 4,4 Z" Fill="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="2,0" />
  </ControlTemplate>

  <Style TargetType="Control" x:Key="{ComponentResourceKey {x:Type src:DataGridFilter}, IconStyle}">
    <Setter Property="HorizontalAlignment" Value="Right" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Template" Value="{DynamicResource {x:Static src:DataGridFilter.IconTemplateKey}}" />
    <Setter Property="Focusable" Value="False" />
    <Setter Property="Foreground" Value="Gray" />
  </Style>

  <!-- Default template for a filter on a column represented by a DataGridTextColumn. -->
  <ControlTemplate x:Key="{ComponentResourceKey {x:Type src:DataGridFilter}, {x:Type DataGridTextColumn}}">
    <Grid>
      <Control Style="{DynamicResource {x:Static src:DataGridFilter.IconStyleKey}}" />
      <TextBox Style="{DynamicResource {x:Static src:DataGridFilter.ColumnHeaderSearchTextBoxStyleKey}}"
               Text="{Binding Path=Filter, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
  </ControlTemplate>

  <!-- Default template for a filter on a column represented by a DataGridCheckBoxColumn. -->
  <ControlTemplate x:Key="{ComponentResourceKey {x:Type src:DataGridFilter}, {x:Type DataGridCheckBoxColumn}}">
    <Grid>
      <Control Style="{DynamicResource {x:Static src:DataGridFilter.IconStyleKey}}" />
      <CheckBox Style="{DynamicResource {x:Static src:DataGridFilter.ColumnHeaderSearchCheckBoxStyleKey}}"
                IsChecked="{Binding Path=Filter, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
  </ControlTemplate>

  <!-- Default template for a filter on a column represented by a DataGridTemplateColumn. -->
  <ControlTemplate x:Key="{ComponentResourceKey {x:Type src:DataGridFilter}, {x:Type DataGridTemplateColumn}}">
    <Grid>
      <Control Style="{DynamicResource {x:Static src:DataGridFilter.IconStyleKey}}" />
      <TextBox Style="{DynamicResource {x:Static src:DataGridFilter.ColumnHeaderSearchTextBoxStyleKey}}"
               Text="{Binding Path=Filter, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
  </ControlTemplate>

  <!-- Default template for the column header including the filter control. -->
  <DataTemplate x:Key="{ComponentResourceKey {x:Type src:DataGridFilter}, ColumnHeaderTemplate}">
    <DockPanel Background="Transparent">
      <!-- The control to host the filter UI for this column -->
      <src:DataGridFilterColumnControl DockPanel.Dock="Right" />
      <ContentPresenter x:Name="PART_Content" Content="{Binding}"
                        SnapsToDevicePixels="{Binding SnapsToDevicePixels, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}"
                        HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}"
                        VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}" />
    </DockPanel>
  </DataTemplate>

  <Style TargetType="ToolTip" x:Key="{ComponentResourceKey {x:Type behaviors:ExtendedStarSizeBehavior}, ColumnHeaderGripperToolTipStyle}">
    <Setter Property="ContentTemplate">
      <Setter.Value>
        <DataTemplate>
          <TextBlock>
            <Run Text="+Ctrl => Apply star sizes to right columns" /><LineBreak /><Run Text="+Shift => Extend beyond available size" />
          </TextBlock>
        </DataTemplate>
      </Setter.Value>
    </Setter>
  </Style>

</ResourceDictionary>